import flask
from flask import Flask
from flask.globals import request
from flask_cors import CORS
from chessboard import *
import json

app = Flask(__name__, static_folder='dist', template_folder='dist', static_url_path='')
CORS(app, supports_credenials=True)
gameServer = None

BLACK = 1
WHITE = 2
YELLOW =3
def ReturnMsg(succ,Msg="",win=None):
    msg = {"succ":succ,"msg":Msg,"win":win}
    return msg


class Game:    #生成一个新棋盘并开始棋局
    def __init__(self):
        self.chessboard = ChessBoard()
        self.whiteList = []     #白棋落子顺序
        self.blackList = []
        self.yellowList = []
        self.blackStep = 0
        self.whiteStep = 0
        self.yellowStep = 0

    def PutOne(self,x,y,state):   #落子，state=1表示黑方落子，state=2表示白方落子 ,state=3表示黄方落子
        if not self.chessboard.get_xy_on_logic_state(x,y):
            if state == BLACK:
                self.chessboard.draw_xy(x,y,state)
                self.blackList.append((x,y))
            elif state == WHITE:
                self.chessboard.draw_xy(x,y,state)
                self.whiteList.append((x,y))
            elif state == YELLOW:
                self.chessboard.draw_xy(x,y,state)
                self.yellowList.append((x,y))
            if(self.chessboard.anyone_win(x,y)):
                gameServer.game_status = state+1
                return 1   #有人胜利
            else:
                return 0   #正常下棋
        else:
            return -1      #此位置已被占用，不可以落子

    def Restart(self):
        self.chessboard.reset()

    def getLast(self,color):
        if color == BLACK:
            if 2*len(self.blackList) != self.blackStep:
                self.blackStep += 1
                return self.blackList[-1]
            else:
                return None
        elif color == WHITE:
            if 2*len(self.whiteList) != self.whiteStep:
                self.whiteStep += 1
                return self.whiteList[-1]
            else:
                return None
        elif color == YELLOW:
            if 2*len(self.yellowList) != self.yellowStep:
                self.yellowStep += 1
                return self.yellowList[-1]
            else:
                return None

    def getHistory(self,step):
        return {"black":self.blackList[-step:],"white":self.whiteList[-step:],"yellow":self.yellowList[-step:]}



class GameServer:    #server的主体
    def __init__(self):
        self.black = None   #两方ip
        self.white = None
        self.yellow = None
        self.current = None       #True表示当前是黑方落子，False代表为白方落子
        self.game_status = 0     #0表示未开始，1表示进行中，2表示黑方胜，3表示白方胜,4代表黄方胜
        self.Game = Game()
        


    def GetIntoGame(self,ip,color):    #加入游戏
        if self.game_status:
            return ReturnMsg(False,"Game has already Start")
        if color == BLACK or color == WHITE or color == YELLOW:
            if color == BLACK:
                if self.black is None or self.black == ip or ip == None:
                    self.black = ip
                    return ReturnMsg(True,"")
                else:
                    return ReturnMsg(False,"Black is already in")   #当前黑方已经加入
            elif color == WHITE:
                if self.white is None or self.white == ip or ip == None:
                    self.white = ip
                    return ReturnMsg(True,"")
                else:
                    return ReturnMsg(False,"White is already in")   #当前白方已经加入
            elif color == YELLOW:
                if self.yellow is None or self.yellow == ip or ip == None:
                    self.yellow = ip
                    return ReturnMsg(True,"")
                else:
                    return ReturnMsg(False,"Yellow is already in")   #当前黄方已经加入



    def GetBoard(self):      #获取当前棋盘状态
        return json.dumps({"board":self.Game.chessboard.board()})
        
    def GameStatus(self):    #游戏状态
        return ReturnMsg(self.game_status,"")

    def GameStart(self):    #开始游戏
        self.current = BLACK       
        # if self.black and self.white and not self.gameStart:
        if self.black and not self.game_status:
            self.game_status = 1
            self.Game = Game()
            return ReturnMsg(True,"")
        else:
            return ReturnMsg(False,"Wait For People")

    def Putdown(self,x,y,state):    ##落子，state=1表示黑方落子，state=2表示白方落子 ,state=3表示黄方落子
        if self.current == state:
            result = self.Game.PutOne(x,y,state)
            self.current = self.current%3+1   #切换持方
        if result == 1:
            return ReturnMsg(True,"Game End",state)
        elif result == 0:
            return ReturnMsg(True,"")
        else:
            return ReturnMsg(False,"This position was occupied")


@app.route("/Game/Ready/feedback",methods=['GET'])    #获取当前游戏准备状态
def game_ready_fdb(): 
    msg = {"black":gameServer.black,"white":gameServer.white,"yellow":gameServer.yellow,"status":gameServer.game_status}
    return json.dumps(msg)

@app.route("/Game/Status/feedback",methods=['POST'])    #获取当前的游戏状态
def game_status_fdb():
    msg = {"current":gameServer.current,"status":gameServer.game_status}
    print(request.get_data())
    req = json.loads(request.get_data())
    color = req["color"]
    if color == BLACK:
        i = gameServer.Game.getLast(WHITE)
        j = gameServer.Game.getLast(YELLOW)
        if i is not None:
            msg["last"] = i
        elif j is not None:
            msg["last"] = j
        else:
            msg["last"] = None


    if color == WHITE:
        i = gameServer.Game.getLast(BLACK)
        j = gameServer.Game.getLast(YELLOW)
        if i is not None:
            msg["last"] = i
        elif j is not None:
            msg["last"] = j
        else:
            msg["last"] = None

    if color == YELLOW:
        i = gameServer.Game.getLast(WHITE)
        j = gameServer.Game.getLast(BLACK)
        if i is not None:
            msg["last"] = i
        elif j is not None:
            msg["last"] = j
        else:
            msg["last"] = None
    return json.dumps(msg)


        


# @app.route("/GameStatus/check",methods=['GET'])
# def check_status():
#     global gameServer
#     return json.dumps(gameServer.GameStatus())

@app.route("/Game/Start",methods=["GET"])    #开始游戏
def game_start():
    return json.dumps(gameServer.GameStart())


@app.route("/Game/GetIn",methods=['POST'])    #进入游戏
def AccessGame():
    msg = json.loads(request.get_data())
    ip = msg["ip"]
    color = msg["color"]   
    res = gameServer.GetIntoGame(ip,color)
    return json.dumps(res)

@app.route("/board/Get",methods=['GET'])    #获取棋盘
def get_board():
    return gameServer.GetBoard()

@app.route("/board/getHistory/<step>",methods=["GET"])    #获取历史步
def get_history(step):
    step = int(step)
    return json.dumps(gameServer.Game.getHistory(step))

@app.route("/board/Put",methods=['POST'])    #下棋
def PutOnce():
    msg = json.loads(request.get_data())
    color = msg["color"]
    x = msg["x"]
    y = msg["y"]
    if color == BLACK:
        return json.dumps(gameServer.Putdown(x,y,BLACK))
    elif color == WHITE:
        return json.dumps(gameServer.Putdown(x,y,WHITE))
    elif color == YELLOW:
        return json.dumps(gameServer.Putdown(x,y,YELLOW))
    else:
        return ReturnMsg(False,"You Are Not In This Game")

@app.route("/Game/Reset",methods=["GET"])
def Reset():   #重新开始游戏
    """
    需要自行完成
    """
    pass
@app.route("/")
def hello():
    return "hello world!"
    
if __name__ == '__main__':
    gameServer = GameServer()
    app.run(
        host='0.0.0.0',
        port='8088',
        debug=True
    )
            


            
            

